<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      label-width="auto"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="车间名称" prop="workshopName">
        <el-select
          v-model="queryParams.workshopName"
          clearable
          style="width: 180px"
          placeholder="请选择"
        >
          <el-option
            v-for="item in workshopOptions"
            :key="item.name"
            :label="item.name"
            :value="item.name"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="生产单编码" prop="produceNo">
        <el-input
          v-model.trim="queryParams.produceNo"
          style="width: 180px"
          placeholder="请输入生产单编码"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报工单编码" prop="code">
        <el-input
          v-model.trim="queryParams.code"
          style="width: 180px"
          placeholder="请输入报工单编码"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-row>
        <el-form-item label="报工日期" prop="reportDate">
          <el-date-picker
            style="width: 180px"
            v-model.trim="queryParams.reportDate"
            value-format="YYYY-MM-DD"
            type="date"
            placeholder="请选择日期"
          />
        </el-form-item>
        <el-form-item label="报工类型" prop="type">
          <el-select
            v-model="queryParams.type"
            @change="handleQuery"
            style="width: 180px"
            placeholder="请选择"
          >
            <el-option
              v-for="item in typeList"
              :key="item.label"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-row>
    </el-form>
    <div>
      <el-table
        show-summary
        :summary-method="getSummaries"
        v-loading="loading"
        border
        :data="dataList"
        height="65vh"
      >
        <el-table-column label="序号" width="60" type="index" align="center" />
        <el-table-column
          label="报工类型"
          min-width="100"
          align="center"
          :show-overflow-tooltip="true"
          prop="type"
          v-if="columns[0].visible"
        >
          <template v-slot="{ row }">
            {{ typeList.find((item) => item.value === row.type).label }}
          </template>
        </el-table-column>
        <el-table-column
          label="状态"
          min-width="100"
          align="center"
          :show-overflow-tooltip="true"
          prop="status"
          v-if="columns[0].visible"
        >
          <template v-slot="{ row }">
            <dict-tag :options="report_status" :value="row.status" />
          </template>
        </el-table-column>
        <el-table-column
          label="报工单编码"
          min-width="200"
          align="center"
          :show-overflow-tooltip="true"
          prop="code"
          v-if="columns[1].visible"
        >
          <template v-slot="{ row }">
            <el-button type="primary" @click="handleView(row)" text>{{
              row.code
            }}</el-button>
          </template>
        </el-table-column>
        <el-table-column
          label="生产单编码"
          min-width="200"
          align="center"
          :show-overflow-tooltip="true"
          prop="produceNo"
          v-if="columns[1].visible"
        >
          <template v-slot="{ row }">
            <div>
              <span
                v-for="(item, index) in row.produceNo?.split(',')"
                :key="index"
              >
                <el-button
                  type="primary"
                  @click="handleViewProduce(row, index)"
                  text
                  >{{ item }}</el-button
                >
                <br />
              </span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="计件工资（元）"
          width="100"
          align="center"
          :show-overflow-tooltip="true"
          prop="wagesPiece"
          v-if="columns[1].visible"
        >
        </el-table-column>
        <el-table-column
          label="机台"
          align="center"
          min-width="220"
          prop="machineName"
          v-if="columns[4].visible"
        >
        </el-table-column>
        <el-table-column
          label="车间"
          align="center"
          min-width="100"
          prop="workshopName"
          v-if="columns[4].visible"
        >
        </el-table-column>
        <el-table-column
          label="创建人"
          min-width="100"
          align="center"
          :show-overflow-tooltip="true"
          prop="createBy"
          v-if="columns[3].visible"
        >
        </el-table-column>
        <el-table-column
          label="创建日期"
          width="120"
          align="center"
          :show-overflow-tooltip="true"
          prop="createTime"
          v-if="columns[2].visible"
        >
          <template v-slot="{ row }">
            {{ parseTime(row.createTime) }}
            <!-- {{ row.createTime ? row.createTime.split("T")[0] : "" }} -->
          </template>
        </el-table-column>
        <!-- v-hasPermi="['production:repeated:del']" -->
        <!-- <el-table-column
          label="操作"
          align="center"
          width="100"
          fixed="right"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-button link type="danger" @click="handleDel(scope.row)"
              >作废</el-button
            >
          </template>
        </el-table-column> -->
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script setup name="Repeated">
import { downLoadBlob, toNumber } from "@/utils/ruoyi";
import { getMesWorkShop } from "@/api/basicInfo/workshop.js";
import {
  getRepeatList,
  updateRepeatStatus,
} from "@/api/production/repeated.js";
import useUserStore from "@/store/modules/user";
import { useRoute } from "vue-router";
import { onActivated } from "@vue/runtime-core";
const { proxy } = getCurrentInstance();
const loading = ref(true);
const dataList = ref([]);
const showSearch = ref(true);
const total = ref(0);
const router = useRouter();
const route = useRoute();
const workshopOptions = ref([]);
// 列显隐信息
const columns = ref([
  { key: 0, label: `状态`, visible: true },
  { key: 1, label: `报工单编码`, visible: true },
  { key: 2, label: `报工日期`, visible: true },
  { key: 3, label: `上报人人`, visible: true },
  { key: 4, label: `机台`, visible: true },
  { key: 5, label: `机长`, visible: true },
  { key: 6, label: `当班日期`, visible: true },
  { key: 7, label: `备注`, visible: true },
  { key: 8, label: `创建人`, visible: true },
  { key: 9, label: `创建时间`, visible: true },
]);
const typeList = ref([
  {
    label: "印刷报工",
    value: "1",
  },
  {
    label: "折页报工",
    value: "2",
  },
  {
    label: "裁切报工",
    value: "3",
  },
  {
    label: "覆膜报工",
    value: "4",
  },
  {
    label: "装订报工",
    value: "5",
  },
  {
    label: "其他报工",
    value: "6",
  },
]);
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 50,
    type: "1",
  },
});

const { queryParams } = toRefs(data);
const { report_status } = proxy.useDict("report_status");
// 权限
const permissions = useUserStore().roles;
if (permissions.includes("chejian")) {
  queryParams.value.workshopName = useUserStore().deptName;
}

const getSummaries = (param) => {
  const { columns, data } = param;
  const sums = [];
  const fieldsToSum = ["status", "type"];
  columns.forEach((column, index) => {
    if (index === 0) {
      sums[index] = "合计";
      return;
    }
    if (!fieldsToSum.includes(column.property)) {
      const values = data.map((item) => Number(item[column.property]));
      if (!values.every((value) => isNaN(value))) {
        sums[index] = values.reduce((prev, curr) => {
          const value = Number(curr);
          if (!isNaN(value)) {
            return toNumber(prev + curr, 2);
          } else {
            return toNumber(prev, 2);
          }
        }, 0);
        sums[index];
      } else {
        sums[index] = "";
      }
    }
  });
  return sums;
};
const getList = () => {
  loading.value = true;
  getRepeatList(queryParams.value)
    .then((res) => {
      loading.value = false;
      dataList.value = res.rows;
      total.value = res.total;
    })
    .catch((err) => {
      loading.value = false;
    });
};

// 查看
const handleViewProduce = (row, index) => {
  const produceId = row.produceId?.split(",")[index];
  const produceNo = row.produceNo?.split(",")[index];
  if (produceNo.indexOf("FM") > -1) {
    router.push({
      path: "/production/cover/coverDetails",
      query: {
        id: produceId,
        info: true,
        moduleCode: "printReport",
      },
    });
  } else {
    router.push({
      path: "/production/text/textDetails",
      query: {
        id: produceId,
        info: true,
        moduleCode: "printReport",
      },
    });
  }
};

// 查看
const handleView = (row) => {
  if (row.type == 1) {
    router.push({
      path: "/production/printReport/printReportDetails",
      query: {
        id: row.id,
        info: true,
      },
    });
  } else if (row.type == 2) {
    router.push({
      path: "/production/foldingReport/foldReportDetails",
      query: {
        id: row.id,
        info: true,
      },
    });
  } else if (row.type == 3) {
    router.push({
      path: "/production/cuttingReport/detail",
      query: {
        id: row.id,
        info: true,
      },
    });
  } else if (row.type == 4) {
    router.push({
      path: "/production/coverReport/coverReportDetails",
      query: {
        id: row.id,
        info: true,
      },
    });
  } else if (row.type == 5) {
    router.push({
      path: "/production/bindReport/bindReportDetails",
      query: {
        id: row.id,
        info: true,
      },
    });
  } else {
    router.push({
      path: "/production/otherReport/otherReportDetail",
      query: {
        id: row.id,
        info: true,
      },
    });
  }
};
// 搜索
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  proxy.resetForm("queryRef");
  handleQuery();
};
/** 作废按钮操作 */
function handleDel(row) {
  proxy.$modal
    .confirm('是否确认作废"' + row.code + '"的报工单？')
    .then(function () {
      return updateRepeatStatus({ id: row.id, status: 3, type: row.type });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("作废成功");
    })
    .catch(() => {});
}

const getWorkShopList = async () => {
  const res = await getMesWorkShop({ pageNum: 1, pageSize: 10 });
  workshopOptions.value = [...res.rows];
};
getWorkShopList();

onActivated(() => {
  getList();
});
</script>

<style lang="scss" scoped></style>
